<?php

/**
 * @author NNDUC
 * @copyright 2012
 */

include_once (SYSTEM . '/Controller.php');
include_once (MODEL . '/WantRepository.php');
include_once (APPLICATION .'/Common/Paging.php');

class WantController extends Controller
{    
    public function ListWant() 
    {
    	include_once (MODEL . '/WantModel.php');
    	
    	$this->ViewData['Title'] = 'Want'; 
    	$want = new WantRepository();        
    	$params = $this->GetParams();
    	
    	$currentCategory = isset($params['category']) ? $params['category'] : '4faa9b416803fa1b05000000';                
        $currentPage = isset($params['page']) ? $params['page'] : 1;       
        $inactivePageTag = 'id="currentPage"';                		
		$pageUrl = '/Want/ListWant?category=' . $currentCategory . '&page=';
		$pageUrllast = '';
		
        $paging =  new Paging();               
        $paging->PageSet($pageUrl, $want->TotalByCategory($currentCategory), $currentPage, $inactivePageTag, $pageUrllast);        
        $model['result'] = $want->ViewListWantByCategory($currentCategory, $paging->start, $paging->perPage);               
        $model['paging'] = $paging;
        
        $this->ViewData['listCat'] = $want->GetListCategory();
                
        $this->Render('ListWant', '_Master', $model);
    }
    
    public function CreateWant() 
    {
    	$this->ViewData['Title'] = 'Create Want';
		$params = $this->GetParams();
		$want = new WantRepository();
		    	
    	if(!isset($params['submit'])) 
    	{
    		$this->ViewData['listCat'] = $want->GetListCategory(); 
    		$this->Render('CreateWant', '_Master');
    	}
    	else 
    	{
	    	include_once (MODEL . '/CreateNewModel.php');	    		    
	    	
	    	$createModel = new CreateNewModel($params);
	    	$message = $createModel->ValidateFields();
	    	if(empty($message))
	    	{	    			    			    	
	    		if(empty($createModel->Image['name'])) 
	    		{
	    			$createModel->Image['name'] = 'Wish.jpg';
	    			$want->CreateWant($createModel);
	    			
	    			$this->Redirect('/Want/ListWant');
	    		}
	    		else 
	    		{
	    			include_once(ROOT.'/Config.php');
	    			$extension = array('image/jpeg', 'image/png', 'image/gif', 'image/bmp');
	    			$dest = UPLOAD.'/Want/';
	    				    			
	    			if(in_array($createModel->Image['type'], $extension) && $createModel->Image['size'] < WebConfig::$AppSetting['LimitSize'] * 1024 * 1024) 
	    			{
	    				$createModel->Image['name'] = time() . $createModel->Image['name'];
	    				$dest .= $createModel->Image['name'];	    				
	    				move_uploaded_file($createModel->Image['tmp_name'], $dest);
	    				$want->CreateWant($createModel);
	    				
	    				$this->Redirect('/Want/ListWant');	    				
	    			}	
	    		}
	    	}
	    	else 
	    	{
	    		$this->Render('CreateOffer', '_Master');	
	    	} 
    	}
    }
    public function DeleteWant()
    {
        $params = $this->GetParams();
        $want = new WantRepository();
        if(isset($params['submit']))
        {
            $filter = array('_id'=> new MongoId($params['wantID']) );
            $want->DeleteWant($filter);
        }
        $this->ListWant();
        
    }
    public function WantDetail()
    {
        $this->ViewData['Title'] = 'Want Detail';
        $params = $this->GetParams();
        $want = new WantRepository();
        $wantID = $params['wantID'];
        $model['result'] = $want->GetDetail($wantID);
        $this->ViewData['listCat'] = $want->GetListCategory();
        $this->Render('WantDetail','_Master',$model);
        
    }
    public function EditWant()
    {
        $this->ViewData['Title'] = 'Edit Want';
		$params = $this->GetParams();
		$want = new WantRepository();
		    	
    	if(isset($params['submit'])) 
    	{
            $wantID = $params['wantID'];
            $model['result'] = $want->GetDetail($wantID);
            $this->ViewData['listCat'] = $want->GetListCategory(); 
            
            $this->Render('EditWant', '_Master',$model);
    	}
    	if(isset($params['edit'])) 
    	{
    		$wantLocation = $params['Location'];
    		$wantID = $params['wantID'];
    		$wantName = $params['Name'];
    		$wantCategory = $params['Category'];
    		$wantDescription =$params['Description'];
    		if ((($_FILES["file"]["type"] == "image/gif")
    				|| ($_FILES["file"]["type"] == "image/jpeg")
    				|| ($_FILES["file"]["type"] == "image/pjpeg"))
    				&& ($_FILES["file"]["size"] < 1000000))
    		{
    		
    			if ($_FILES["file"]["error"] > 0)
    			{
    				$this->ViewData['error']= "Error: " . $_FILES["file"]["error"] . "<br />";
    				$this->Render('editImage','_Master');
    			}
    			else{
    				$existFile = UPLOAD."/Want/".$wantID.".jpg";
    				if(file_exists($existFile))
    				{
    					unlink($existFile);
    				}
    				move_uploaded_file($_FILES["file"]["tmp_name"],$existFile);
    		
    			}
    		}
    		else
    		{
    			$this->ViewData['error']= "khong dung dinh dang file hoac dung luong qua lon";
    			$model['result'] = $want->GetDetail($wantID);
    			$this->ViewData['listCat'] = $want->GetListCategory();
    			 
    			$this->Render('EditWant', '_Master',$model);
    		}
    		
    		$want->EditWant($wantID, $wantName, $wantCategory, $wantDescription, $wantLocation);
    		$this->Redirect('/Want/ListWant');    		
	    	
    	}
    }
}